package org.lg.controller;

import org.lg.entity.Item;
import org.lg.service.impl.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/items")
public class ItemController {

    @Autowired
    private ItemService itemService;

    // 查询所有道具
    @GetMapping("/")
    public ResponseEntity<Map<String, Object>> getAllItems() {
        List<Item> items = itemService.getAllItems();
        Map<String, Object> response = new HashMap<>();
        response.put("$", items);  // 使用 "$" 包装道具列表
        return ResponseEntity.ok(response);
    }

    // 根据ID查询道具
    @GetMapping("/{id}")
    public ResponseEntity<Map<String, Object>> getItemById(@PathVariable String id) {
        Item item = itemService.getItemById(id);
        Map<String, Object> response = new HashMap<>();
        Map<String, Object> itemData = new HashMap<>();
        itemData.put("item", item);  // 将道具信息包装在 "item" 字段中
        response.put("$", itemData);  // 将整个返回包装在 "$" 字段中
        return ResponseEntity.ok(response);
    }

    // 添加道具
    @PostMapping("/")
    public ResponseEntity<Map<String, Object>> addItem(@RequestBody Item item) {
        String result = itemService.addItem(item);
        Map<String, Object> response = new HashMap<>();
        response.put("message", result);  // 返回操作结果的消息
        return ResponseEntity.ok(response);
    }

    // 更新道具
    @PutMapping("/")
    public ResponseEntity<Map<String, Object>> updateItem(@RequestBody Item item) {
        String result = itemService.updateItem(item);
        Map<String, Object> response = new HashMap<>();
        response.put("message", result);  // 返回操作结果的消息
        return ResponseEntity.ok(response);
    }

    // 删除道具
    @DeleteMapping("/{id}")
    public ResponseEntity<Map<String, Object>> deleteItem(@PathVariable String id) {
        String result = itemService.deleteItem(id);
        Map<String, Object> response = new HashMap<>();
        response.put("message", result);  // 返回操作结果的消息
        return ResponseEntity.ok(response);
    }
}

